package com.msos.dao.phoneDeleteDao;

import com.msos.model.customer.Customer;
import com.msos.utils.CommonsUtils;
import com.msos.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class phoneDeleteDao {
    QueryRunner queryRunner = new QueryRunner(DBUtils.getDs());
    public Integer deleteSelectPhos(String[] numList) throws SQLException {
        Connection con = DBUtils.getCon();
        StringBuffer sql=new StringBuffer("update customer set enabled=0  where phone in(");
        for(int i=0;i<numList.length;i++){
            if(i==numList.length-1){
                sql.append("?)");
            }else {
                sql.append("?,");
            }
        }
        PreparedStatement ps = con.prepareStatement(sql.toString());
        for(int i=0;i<numList.length;i++){
            ps.setString(i+1,numList[i]);
        }
        Integer r1=ps.executeUpdate();
        System.out.println("（将customer置为不可用）修改记录条数r1==="+r1);
        DBUtils.close(ps);
        return r1>0? CommonsUtils.SUCCESS:CommonsUtils.FAIL;
    }

    public List<Customer> getAllCus() throws SQLException {

        return queryRunner.query("select * from customer where enabled=1",new BeanListHandler<>(Customer.class));

    }
}
